package c.j.a.d.a;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import java.util.ArrayDeque;
import java.util.Queue;

/* compiled from: InstallQueue */
public class n {
    public Queue<Integer> a = new ArrayDeque();
    public boolean b = false;
    public long c;
    public long d;
    public Handler e = new Handler(Looper.getMainLooper());
    public Runnable f = new a();

    /* compiled from: InstallQueue */
    public class a implements Runnable {
        public void run() {
            n.this.c();
        }
    }

    /* compiled from: InstallQueue */
    public class b implements Runnable {
        public final /* synthetic */ Context a;
        public final /* synthetic */ Integer b;

        public b(Context context, Integer num) {
            this.a = context;
            this.b = num;
        }

        public void run() {
            n.this.b(this.a, this.b.intValue(), false);
        }
    }

    /* compiled from: InstallQueue */
    public class c implements Runnable {
        public final /* synthetic */ Context a;
        public final /* synthetic */ int b;
        public final /* synthetic */ boolean c;

        public c(Context context, int i, boolean z) {
            this.a = context;
            this.b = i;
            this.c = z;
        }

        public void run() {
            n.this.a(this.a, this.b, this.c);
        }
    }

    /* compiled from: InstallQueue */
    public static class d {
        public static final n a = new n(null);
    }

    public n(a aVar) {
        c.j.a.d.b.a.a.c.a.a(new o(this));
    }

    public int a(Context context, int i, boolean z) {
        if (z) {
            return b(context, i, z);
        }
        if ((System.currentTimeMillis() - this.c < 1000 ? 1 : null) != null) {
            this.e.postDelayed(new c(context, i, z), 1000);
            return 1;
        } else if (c.j.a.d.b.a.a.c.a.c()) {
            return b(context, i, z);
        } else {
            if (this.a.isEmpty() && !this.b) {
                return b(context, i, z);
            }
            int b = c.j.a.d.b.l.a.f.b("install_queue_size", 3);
            while (this.a.size() > b) {
                this.a.poll();
            }
            this.e.removeCallbacks(this.f);
            this.e.postDelayed(this.f, c.j.a.d.b.l.a.d(i).c("install_queue_timeout", 20000));
            if (!this.a.contains(Integer.valueOf(i))) {
                this.a.offer(Integer.valueOf(i));
            }
            return 1;
        }
    }

    public final int b(Context context, int i, boolean z) {
        int k = h.k(context, i, z);
        if (k == 1) {
            this.b = true;
        }
        this.c = System.currentTimeMillis();
        return k;
    }

    public final void c() {
        Integer num = (Integer) this.a.poll();
        this.e.removeCallbacks(this.f);
        if (num != null) {
            Context a = c.j.a.d.b.e.b.a();
            if (Looper.myLooper() != Looper.getMainLooper()) {
                this.e.post(new b(a, num));
            } else {
                b(a, num.intValue(), false);
            }
            this.e.postDelayed(this.f, 20000);
            return;
        }
        this.b = false;
    }
}
